﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class SignagesRemarkRepo
    {

        public const string m_msg_head = "SignagesRemark";

        public IEnumerable<SignagesRemarkModels> GetSignagesRemark()
        {
            List<SignagesRemarkModels> SignagesRemarks = new List<SignagesRemarkModels>();
            string SQLCommand = @"select * from TBSignagesRemark";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        SignagesRemarkModels SignagesRemark = new SignagesRemarkModels();
                        SignagesRemark.Id = (int)reader["Id"];
                        SignagesRemark.SignagesRemarkTh = (string)reader["SignagesRemarkTh"];
                        SignagesRemark.SignagesRemarkEng = (string)reader["SignagesRemarkEng"];
                        SignagesRemark.Status = (string)reader["Status"];
                        SignagesRemark.CreateBy = (string)reader["CreateBy"];
                        SignagesRemark.CreateOn = (DateTime)reader["CreateOn"];
                        SignagesRemark.UpdateBy = (string)reader["UpdateBy"];
                        SignagesRemark.UpdateOn = (DateTime)reader["UpdateOn"];

                        SignagesRemarks.Add(SignagesRemark);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return SignagesRemarks.ToList();
        }

        public SignagesRemarkModels GetSignagesRemarkById(int id)
        {
            SignagesRemarkModels SignagesRemark = new SignagesRemarkModels();
            string SQLCommand = @"select * from TBSignagesRemark where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@Id", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        SignagesRemark.Id = (int)reader["Id"];
                        SignagesRemark.SignagesRemarkTh = (string)reader["SignagesRemarkTh"];
                        SignagesRemark.SignagesRemarkEng = (string)reader["SignagesRemarkEng"];
                        SignagesRemark.Status = (string)reader["Status"];
                        SignagesRemark.CreateBy = (string)reader["CreateBy"];
                        SignagesRemark.CreateOn = (DateTime)reader["CreateOn"];
                        SignagesRemark.UpdateBy = (string)reader["UpdateBy"];
                        SignagesRemark.UpdateOn = (DateTime)reader["UpdateOn"];
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return SignagesRemark;
        }

        public bool Create(SignagesRemarkModels SignagesRemark)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBSignagesRemark
                                    values (@SignagesRemarkTh,@SignagesRemarkEng,@Status,@UserId,
                                    GETDATE(),@UserId,GETDATE())";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@SignagesRemarkTh", SignagesRemark.SignagesRemarkTh);
                    db.AddParameter("@SignagesRemarkEng", SignagesRemark.SignagesRemarkEng);
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID", SignagesRemark.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(SignagesRemarkModels SignagesRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBSignagesRemark 
                                    Set SignagesRemarkTh=@SignagesRemarkTh,
                                        SignagesRemarkEng=@SignagesRemarkEng,
                                        UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", SignagesRemark.Id);
                    db.AddParameter("@SignagesRemarkTh", SignagesRemark.SignagesRemarkTh);
                    db.AddParameter("@SignagesRemarkEng", SignagesRemark.SignagesRemarkEng);
                    db.AddParameter("@UserID", SignagesRemark.UpdateBy );

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(SignagesRemarkModels SignagesRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBSignagesRemark 
                                    Set Status=@Status,UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", SignagesRemark.Id);
                    db.AddParameter("@Status", "Delete");
                    db.AddParameter("@UserID", "900153");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}